import os
from dotenv import load_dotenv
from flask import Flask, request, jsonify
# from deepseek_api import DeepSeekAPI  # 假设这是DeepSeek的官方Python SDK
from openai import OpenAI
from flask_cors import CORS

# pnpm install axios marked dompurify

# conda install flask
# conda install flask-cors
# conda install openai
# pip install requests python-dotenv



app = Flask(__name__)
CORS(app)

# 加载环境变量
load_dotenv()
api_key = os.getenv("DEEPSEEK_API_KEY")

base_url="https://api.deepseek.com"

@app.route('/api/chat', methods=['POST'])
def chat():
    try:
        data = request.json
        messages = data.get('messages', [])
        
        # 调用DeepSeek API
        deepseek = OpenAI(api_key=api_key, base_url=base_url)
        response = deepseek.chat.completions.create(
            model="deepseek-chat",
            messages=messages,
            temperature=0.7,
            stream=False
        )
        
        return jsonify({
            "content": response.choices[0].message.content,
            "status": "success"
        })
        
    except Exception as e:
        return jsonify({
            "content": f"Error: {str(e)}",
            "status": "error"
        }), 500

if __name__ == '__main__':
    app.run(debug=True, port=5000)
